package com.zhxs.utils

import com.yc.ycthreadpoollib.PoolThread
import com.yc.ycthreadpoollib.callback.ThreadCallback

class ThreadPoolHelper private constructor() {

    init {
        initThreadPool()
    }

    companion object {
        private var instance: ThreadPoolHelper? = null
            get() {
                if (field == null) {
                    field = ThreadPoolHelper()
                }
                return field
            }

        fun get(): ThreadPoolHelper {
            //细心的小伙伴肯定发现了，这里不用getInstance作为为方法名，是因为在伴生对象声明时，内部已有getInstance方法，所以只能取其他名字
            return instance!!
        }
    }

    private var executor: PoolThread? = null

    private fun initThreadPool() {
        // 创建一个独立的实例进行使用
        executor = PoolThread.ThreadBuilder
            .createFixed(5)
            .setPriority(Thread.MAX_PRIORITY)
            .setCallback(LogCallback())
            .build()
    }

    /**
     * 获取线程池管理器对象，统一的管理器维护所有的线程池
     * @return executor对象
     */
    fun getExecutor(): PoolThread? {
        return executor
    }

    //自定义回调监听callback，可以全局设置，也可以单独设置。都行
    class LogCallback : ThreadCallback {
        override fun onError(name: String, t: Throwable) {

        }

        override fun onCompleted(name: String) {
        }

        override fun onStart(name: String) {
        }
    }
}